<template>
  <div class="login-container">
    <div class="form-box">
      <h1>黑马面经运营后台</h1>
      <el-form
        :model="ruleForm"
        :rules="rules"
        class="demo-ruleForm"
        ref="ruleForm"
        status-icon
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            autocomplete="off"
            placeholder="请输入用户名"
            type="password"
            v-model="ruleForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            autocomplete="off"
            placeholder="请输入密码"
            type="password"
            v-model="ruleForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="submitForm('ruleForm')"
            class="submit"
            type="primary"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback()
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: 'admin',
        password: 'admin'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ],
        password: [
          { validator: validatePass2, trigger: 'blur', required: true, message: '请输入密码' }
        ]
      }
    };
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          await this.$store.dispatch('user/LoginEvent', this.ruleForm)
          this.$router.push('/')
        } else {
          console.log('error submit!!')
          return false;
        }
      });
    },
    resetForm (formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
<style lang='scss'  scoped>
.login-container {
  background: url(~@/assets/login-bg.svg) no-repeat center / cover;
  width: 100vw;
  height: 100vh;
  background-color: pink;
  position: relative;
  .form-box {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 35%;
    background-color: #fff;
    .el-form {
      margin: 60px 40px;
      button {
        font-size: 20px;
        &.submit {
          background-color: #727cf5;
        }
        padding: 18px 30px;
      }
    }
    h1 {
      text-align: center;
      background-color: #727cf5;
      margin: 0;
      padding: 20px 0;
      color: #fff;
      font-weight: 400;
    }
  }
}
</style>